package cur.nature.starter.common.filter;


import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cur.nature.framework.core.exception.BizException;
import cur.nature.framework.core.exception.SystemException;
import cur.nature.framework.core.log.LOG;
import cur.nature.framework.mvc.http.ResponseObject;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.validation.method.ParameterErrors;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.HandlerMethodValidationException;

@RestControllerAdvice
public class CustomControllerAdvice {
    @Order(0)
    @ExceptionHandler(value = BizException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseObject bizException(Throwable ex) {
        LOG.warn("业务异常:",ex);
        return ResponseObject.systemErr("业务异常："+(StringUtils.isEmpty(ex.getMessage())? "":ex.getMessage()));
    }

    @Order(0)
    @ExceptionHandler(value = SystemException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseObject sysException(Throwable ex) {
        LOG.warn("系统异常:",ex);
        return ResponseObject.systemErr("操作异常："+(StringUtils.isEmpty(ex.getMessage())? "":ex.getMessage()));
    }

    @Order(0)
    @ExceptionHandler(value = NotLoginException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public ResponseObject notLoginException(Throwable ex) {
        LOG.warn("未登录异常:",ex);
        return ResponseObject.unAuthorizeErr("未登录异常,"+(StringUtils.isEmpty(ex.getMessage())? "":ex.getMessage()));
    }

    @Order(0)
    @ExceptionHandler(value = NotPermissionException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public ResponseObject unAuthorizeException(Throwable ex) {
        LOG.warn("未授权异常:",ex);
        return ResponseObject.unAuthorizeErr("未授权异常,"+(StringUtils.isEmpty(ex.getMessage())? "":ex.getMessage()));
    }

    @Order(0)
    @ExceptionHandler(value = {HandlerMethodValidationException.class})
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResponseObject constraintViolationException(HandlerMethodValidationException ex) {
        LOG.warn("参数校验异常:" + ex.getMessage(),ex);
        StringBuilder stringBuilder = new StringBuilder();
        for(ParameterErrors fieldError: ex.getBeanResults()){
            for (ObjectError errorObj : fieldError.getAllErrors()) {
                FieldError error = (FieldError) errorObj;
                stringBuilder.append("字段( "+error.getField()+" )校验失败: "+error.getDefaultMessage()+" ; ");
            }
        }
        String msg = stringBuilder.toString();
        return ResponseObject.validateException(msg);
    }

    @Order(1)
    @ExceptionHandler(value = {Throwable.class,Exception.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseObject throwableException(Throwable ex) throws Throwable {
        LOG.warn("拦截Throwable。。。。。。。",ex);
        return ResponseObject.systemErr("系统异常："+(StringUtils.isEmpty(ex.getMessage())? "":ex.getMessage()));
    }
}
